package org.fast.framework.loader;

import org.fast.framework.config.FastConfig;
import org.fast.framework.config.GlobalConfig;
import org.fast.framework.config.JdbcConfig;
import org.fast.framework.config.MvcConfig;
import org.ho.yaml.Yaml;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileNotFoundException;
import java.net.URL;

/**
 * 属性文件助手类
 */
public final class ConfigLoader {

    private static final Logger LOGGER = LoggerFactory.getLogger(ConfigLoader.class);

    private static final GlobalConfig GLOBAL_CONFIG = loadProps("application.yml");

    private static GlobalConfig loadProps(String propName) {
        URL configUrl = ConfigLoader.class.getClassLoader().getResource(propName);
        GlobalConfig globalConfig = null;
        try {
            globalConfig = Yaml.loadType(new File(configUrl.getPath()), GlobalConfig.class);
        } catch (FileNotFoundException e) {
            LOGGER.error("cannot load " + propName, e);
        }
        return globalConfig;
    }

    public static FastConfig getFastConfig() {
        return GLOBAL_CONFIG.getFast();
    }

    public static JdbcConfig getJdbcConfig() {
        return getFastConfig().getJdbc();
    }

    public static MvcConfig getMvcConfig() {
        return getFastConfig().getMvc();
    }
}
